package cn.tedu.zx.court.adapter.controller;


import cn.tedu.zx.common.restful.JsonPage;
import cn.tedu.zx.common.restful.JsonResult;
import cn.tedu.zx.common.restful.ResponseCode;
import cn.tedu.zx.court.service.CourtService;
import cn.tedu.zx.pojo.court.dto.CourtAddNewDto;
import cn.tedu.zx.pojo.court.dto.CourtUpdateDto;
import cn.tedu.zx.pojo.court.vo.CourtDetailsVo;
import cn.tedu.zx.pojo.court.vo.CourtGroupListVo;
import cn.tedu.zx.pojo.court.vo.CourtListVo;
import cn.tedu.zx.pojo.court.vo.CourtUserWatchListVo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/court/")
//通过knife4j配置IPI文档
@Api(tags = "03.场地模块")
@Validated/*未封装参数检查注解*/
public class CourtController {
    @Autowired
    private CourtService courtService;

    @PostMapping("/addNew")
    @ApiOperation("添加场地")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAnyAuthority('/court/insert')")
    /*@ApiIgnore注解忽略注解后的请求参数*/
    public JsonResult addNew(@Valid CourtAddNewDto courtAddNewDto) {
        log.debug("开始执行{}", courtAddNewDto);
        courtService.addNew(courtAddNewDto);
        log.debug("添加完成");
        return JsonResult.ok();//静态方法
    }

    @PostMapping("/delete")
    @ApiOperation("根据id删除")
    @ApiOperationSupport(order = 300)
    @PreAuthorize("hasAnyAuthority('/court/delete')")
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "id", value = "场地id", required = true, dataType = "Long")}
    )
    public JsonResult delete(@Range(min = 1, message = "Id异常，请添加正确的id") @RequestParam Long id) {
        courtService.deleteById(id);
        return JsonResult.failed(ResponseCode.OK, "成功删除");
    }

    @GetMapping("/selectDetails")
    @ApiOperation("根据id查询场地详情")
    @ApiOperationSupport(order = 500)
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "id", value = "分类id", required = true, dataType = "Long")}
    )
    public JsonResult selectByI(@Range(min = 1, message = "Id异常，请添加正确的id") @RequestParam Long id) {
        CourtDetailsVo categoryDetailsVo = courtService.selectDetailsById(id);
        return JsonResult.ok(categoryDetailsVo);
    }

    @PostMapping("/update")
    @ApiOperation("修改场地信息")
    @ApiOperationSupport(order = 200)
    @PreAuthorize("hasAnyAuthority('/court/update')")
    public JsonResult update(@Valid CourtUpdateDto updateDto) {
        courtService.updateById(updateDto);
        return JsonResult.ok();
    }

    @PostMapping("/updateIsEnableById")
    @ApiOperation("修改场地启用状态")
    @ApiOperationSupport(order = 300)
    @PreAuthorize("hasAnyAuthority('/court/update')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "场地id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "isEnable", value = "启用状态码", required = true, dataType = "Integer")
    })
    public JsonResult<Void> updateEnable(@RequestParam Long id, @RequestParam Integer isEnable) {
        courtService.updateIsEnableBuId(id, isEnable);
        return JsonResult.ok();
    }

    @GetMapping("/list")
    @ApiOperation("场地列表")
    @ApiOperationSupport(order = 600)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "场地页码", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示数量", required = true, dataType = "Integer")
    })
    public JsonResult<JsonPage<CourtListVo>> list(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        log.debug("开始处理请求{}", pageNum);
        JsonPage<CourtListVo> jsonPage = courtService.list(pageNum, pageSize);
        return JsonResult.ok(jsonPage);
    }

    @GetMapping("/groupList")
    @ApiOperation(value = "根据类别查场地")
    @ApiOperationSupport(order = 700)
    @ApiImplicitParam(name = "id", value = "分类id", required = true, example = "1")
    public JsonResult<List<CourtGroupListVo>> listByCategoryId(Long id) {
        List<CourtGroupListVo> groupListVos = courtService.listByCategoryId(id);
        return JsonResult.ok(groupListVos);
    }

    @GetMapping("UserWatchList")
    @ApiOperation(value = "用户可见场地列表")
    @ApiOperationSupport(order = 800)
    public JsonResult<List<CourtUserWatchListVo>> UserWatchList() {
        List<CourtUserWatchListVo> list = courtService.UserWatchList();
        return JsonResult.ok(list);
    }
}
